//
//  HeaderView.m
//  庐山通
//
//  Created by zcj on 15-11-2.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "HeaderView.h"
#import "MJExtension.h"
#import "TopSlideCell.h"
#import "HeaderModel.h"
#import <UIKit/UIKit.h>
#import "Grouble.h"

#define  MaxCount 100

@interface HeaderView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,weak) UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,weak) UIPageControl *page;
@property (nonatomic,strong) NSTimer *timer;


@end
@implementation HeaderView

//
- (NSMutableArray *)arr
{
    if (_arr == nil) {
        _arr  = [[NSMutableArray alloc]init];
    }
    return _arr;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addCollection];
        //[self addPage];
        //[self addTimer];
        // Initialization code
    }
    return self;
}
- (void)addTimer
{
    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}
-(NSIndexPath*)resetIndexPath
{
    NSIndexPath *currentIndexPath=[[self.collectionView indexPathsForVisibleItems] lastObject];
    NSIndexPath *currentIndexPathReset=[NSIndexPath indexPathForItem:currentIndexPath.item inSection:MaxCount/2];
    
    [self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    return currentIndexPathReset;
}
- (void)nextPage
{
    
    NSIndexPath  *currentIndexPathReset=[self resetIndexPath];
    NSInteger item=currentIndexPathReset.item; //行
    NSInteger sec=currentIndexPathReset.section; //区
    item++;
    if(item==self.arr.count){
        item=0;
        sec++;
    }
    
    // self.page.currentPage=item; //当前页数
    
    NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:item inSection:sec];
    
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    

}
- (void)addPage
{
    UIPageControl *page=[[UIPageControl alloc]init];
    page.numberOfPages=self.arr.count;
    page.pageIndicatorTintColor=ZCJColor(255, 255, 255);
    
    page.currentPageIndicatorTintColor=ZCJColor(252, 73, 80);
    CGFloat pageW=100;
    CGFloat pageH=30;
    CGFloat pageX=(self.width-pageW)*0.5;
    CGFloat pageY=self.height-pageH+5;
    page.frame=CGRectMake(pageX, pageY, pageW, pageH);
    [self addSubview:page];
    self.page=page;

}
- (void)addCollection
{
    NSArray * myArray = [HeaderModel objectArrayWithFilename:@"newses.plist"];
    
    [self.arr addObject:myArray];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = self.frame.size;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    
    [collectionView registerClass:[TopSlideCell class] forCellWithReuseIdentifier:@"topslide"];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;

    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopSlideCell * cell = [TopSlideCell TopSlideCell:collectionView collectionIndex:indexPath];
    //HeaderModel * headerModel = self.arr[indexPath.item];
   // cell.headerModel = headerModel;
    return cell;
}
#pragma mark 删除控制器的方法
-(void)removeTimer
{
    [self.timer invalidate];
    self.timer=nil;
}
#pragma mark 手指将要托转的时候
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // NSLog(@"scrollViewWillBeginDragging");
    [self removeTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // NSLog(@"scrollViewDidEndDragging");
    //[self addTimer];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // NSLog(@"scrollViewDidEndDecelerating--->");
    [self resetIndexPath];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page=(scrollView.contentOffset.x/scrollView.frame.size.width+0.5);
    int currentPage=page%self.arr.count;
    self.page.currentPage=currentPage;
}
@end
